//
//  SettingVC.m
//  wanzhaoIM
//
//  Created by liuyuantao on 15/5/5.
//  Copyright (c) 2015年 liuyuantao. All rights reserved.
//

#import "EFengAndETongSettingController.h"
#import "MicroDef.h"
#import "SettingTextCell.h"
#import "SettingMidTextCell.h"
#import "MessageSettingVC.h"
#import "SafePrivacyVC.h"
#import "FlowControlVC.h"
#import "OtherSettingsVC.h"
#import "CCurrentAccountMgr.h"
//#import "CAccountModel.h"
//#import "CAccountMgr.h"
#import "LoginViewController.h"
#import "CFolderMgr.h"
#import "CXmppRequestMgr.h"
#import "CXmppMgr.h"
#import "JKAlertDialog.h"
#import "EFShareSettingController.h"
#import "WZOfficeSettingController.h"

#define KSECTIONHEIGHT 20.0f
@interface EFengAndETongSettingController ()<UITableViewDelegate, UITableViewDataSource>
{
    UITableView *_tableView;
}
@end

@implementation EFengAndETongSettingController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)dealloc
{
    //    [[NSNotificationCenter defaultCenter] removeObserver:self name:K_XMPPSETTOKEN object:nil];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    //    [[CXmppRequestMgr shareInstance] getUserSettingInfo];
    
    // Do any additional setup after loading the view.
    [self.navigationController setNavigationBarHidden:NO];
    [self.navigationItem setHidesBackButton:YES];
    [self.navigationItem setNewTitleCenter:@"设置"];
    
    //    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didQuit) name:K_XMPPSETTOKEN object:nil];
    _tableView = [[UITableView alloc] init];
    _tableView.frame = CGRectMake(0, 0, HARDWARE_SCREEN_WIDTH, HARDWARE_SCREEN_HEIGHT - 64.0f);
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
    _tableView.backgroundColor = [UIColor clearColor];
    
    
    self.view.backgroundColor = RGBCOLOR(0xf3, 0xf3, 0xf3);
}


//- (void)didQuit
//{
//    [[CXmppMgr shareInstance] disconnect];
//}


#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    NSInteger rows = 0;
    
    switch (section) {
        case 0:
            rows = 2;
            break;
        case 1:
            rows = 3;
            break;
        case 2:
            rows = 1;
            break;
        case 3:
            rows = 1;
            break;
        default:
            rows = 0;
            break;
    }
    return rows;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView              // Default is 1 if not implemented
{
    return 4;
}
// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"SettingCell";
    static NSString *logoutCellIdentifier = @"SettingMidTextCell";
    UITableViewCell *cell = nil;
    if(indexPath.section != 3)
    {
        //        SettingUserInfoCell.
        
        SettingTextCell *textCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (textCell == nil) {
            textCell = [[[NSBundle mainBundle] loadNibNamed:@"SettingTextCell" owner:self options:nil] objectAtIndex:0];
        }
        if((indexPath.section == 0 && indexPath.row == 0) || (indexPath.section == 1 && indexPath.row == 0))
        {
            [textCell.seperatorLine setHidden:NO];
        }
        else
        {
            [textCell.seperatorLine setHidden:NO];
        }
        [textCell.titleLabel setText:[self titleName:indexPath]];
        cell = textCell;
    }
    else
    {
        SettingMidTextCell *logoutCell = [tableView dequeueReusableCellWithIdentifier:logoutCellIdentifier];
        if (logoutCell == nil) {
            logoutCell = [[[NSBundle mainBundle] loadNibNamed:@"SettingMidTextCell" owner:self options:nil] objectAtIndex:0];
        }
        
        [logoutCell.titleLabel setText:@"退出当前账号"];
        [logoutCell.seperatorLine setHidden:YES];
        logoutCell.accessoryType = UITableViewCellAccessoryNone;
        cell = logoutCell;
    }
    
    return cell;
}

-(NSString *)titleName:(NSIndexPath *)indexPath
{
    NSString *title = nil;
    
    switch (indexPath.section) {
        case 0:
        {
            switch (indexPath.row) {
                case 0:
                    title = @"消息通知";
                    break;
                case 1:
                    title = @"安全与隐私";
                    break;
                    
                default:
                    break;
            }
        }
            break;
        case 1:{
            switch (indexPath.row) {
                case 0:
                    title = @"流量控制";
                    break;
                case 1:
                    title = @"分享设置";
                    break;
                case 2:
                    title = @"办公设置";
                    break;

                default:
                    break;
            }
        }
            break;
        case 2:
            title = @"其他设置";
            break;
            
        default:
            break;
    }
    return title;
}




#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    
    CGFloat height = 0;
    switch (section) {
        case 1:
            height = KSECTIONHEIGHT;
            break;
        case 2:
            height = KSECTIONHEIGHT;
            break;
        case 3:
            height = KSECTIONHEIGHT;
            break;
        default:
            break;
    }
    return height;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    CGFloat height = 0;
    switch (section) {
        case 3:
            height = KSECTIONHEIGHT;
            break;
        default:
            break;
    }
    return height;
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section   // custom view for header. will be adjusted to default or specified header height
{
    UIView *headerView = nil;
    if(section == 0)
    {
        return headerView;
    }
    headerView = [[UIView alloc] init];
    headerView.frame = CGRectMake(0, 0, tableView.frame.size.width, KSECTIONHEIGHT);
    headerView.backgroundColor = RGBCOLOR(0xf3, 0xf3, 0xf3);
    UIView *beginLine = [[UIView alloc] init];
    beginLine.frame = CGRectMake(0, 0, tableView.frame.size.width, 0.5);
    [beginLine setBackgroundColor:KSEPERATORLINECOLOR];
    [headerView addSubview:beginLine];
    
    UIView *endLine = [[UIView alloc] init];
    endLine.frame = CGRectMake(0, KSECTIONHEIGHT-0.5, tableView.frame.size.width, 0.5);
    [endLine setBackgroundColor:KSEPERATORLINECOLOR];
    [headerView addSubview:endLine];
    
    return headerView;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section   // custom view for footer. will be adjusted to default or specified footer height
{
    UIView *headerView = nil;
    if(section != 3)
    {
        return headerView;
    }
    headerView = [[UIView alloc] init];
    headerView.frame = CGRectMake(0, 0, tableView.frame.size.width, 2);
    headerView.backgroundColor = RGBCOLOR(0xf3, 0xf3, 0xf3);
    UIView *beginLine = [[UIView alloc] init];
    beginLine.frame = CGRectMake(0, 0, tableView.frame.size.width, 0.5);
    [beginLine setBackgroundColor:KSEPERATORLINECOLOR];
    [headerView addSubview:beginLine];
    return headerView;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat height = 50.0f;
    
    if(indexPath.row == 0 && indexPath.section == 0)
    {
        height = 50.0f;
    }
    return height;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    switch (indexPath.section) {
        case 0:
        {
            switch (indexPath.row) {
                case 0:
                {
                    [self showMessageSettingVC];
                }
                    break;
                case 1:
                {
                    [self showSafePrivacyVC];
                }
                    break;
                default:
                    break;
            }
        }
            break;
        case 1:
        {
            switch (indexPath.row) {
                case 0:
                {
                    [self showFlowControlVC];;
                }
                    break;
                case 1:
                {
                    [self showShareSettingVc];
                }
                    break;
                case 2:
                {
                    [self showOfficeSettingsVC];
                }
                    break;

                default:
                    break;
            }
            
        }
            break;
        case 2:
        {
            [self showOtherSettingsVC];
        }
            break;
            
        case 3:
        {
            [self quit];
        }
        default:
            break;
    }
    
    
}

- (void)showShareSettingVc
{
    EFShareSettingController *set = [[EFShareSettingController alloc] init];
    [self.navigationController pushViewController:set animated:YES];
}


- (void)quit
{
    //    bug 6010 yh
    JKAlertDialog *alert = [[JKAlertDialog alloc]initWithTitle:nil message:@"确定退出当前账号?"];
    [alert addButton:Button_OK withTitle:@"确定" handler:^(JKAlertDialogItem *item) {
        [[CXmppMgr shareInstance] goOffline];
        [[CXmppMgr shareInstance] disconnect];
        [[CXmppRequestMgr shareInstance] quitLoad];
        
        [USER_DEFAULT setBool:YES forKey:ISFORCEQUIT];
        [USER_DEFAULT synchronize];
        
        //用户退出登录以后必须清空 [CFolderMgr shareInstance].recentlyAcc = nil；
        UINavigationController *rotNav = (UINavigationController *)[UIApplication sharedApplication].keyWindow.rootViewController;
        
        for (UIViewController *vc in rotNav.childViewControllers) {
            if ([vc isKindOfClass:[LoginViewController class]]) {
                LoginViewController *login = (LoginViewController *)vc;
                [login setverifyStatus:NO];
                login.errTimes = 0;
                login.isUseverifyCode = NO;
                [login setErrStatus:NO errorMsg:nil];
            }
        }
        [rotNav popToRootViewControllerAnimated:YES];
        [CFolderMgr shareInstance].recentlyAcc = nil;
    }];
    
    [alert addButton:Button_CANCEL withTitle:@"取消" handler:^(JKAlertDialogItem *item) {
        
        
    }];
    [alert show];
    
}
-(void)showMessageSettingVC
{
    MessageSettingVC *messageSetting = [[MessageSettingVC alloc] init];
    //    CHSNavigationController *messageSettingNav = [[CHSNavigationController alloc] initWithRootViewController:messageSetting];
    //    [self.navigationController presentViewController:messageSettingNav animated:YES completion:nil];
    [self.navigationController pushViewController:messageSetting animated:YES];
}

-(void)showSafePrivacyVC
{
    SafePrivacyVC *safePrivacySetting = [[SafePrivacyVC alloc] init];
    //    CHSNavigationController *safePrivacySettingNav = [[CHSNavigationController alloc] initWithRootViewController:safePrivacySetting];
    //    [self.navigationController presentViewController:messageSettingNav animated:YES completion:nil];
    [self.navigationController pushViewController:safePrivacySetting animated:YES];
}

//FlowControlVC
-(void)showFlowControlVC
{
    FlowControlVC *flowcontrolSetting = [[FlowControlVC alloc] init];
    //    CHSNavigationController *flowcontrolSettingNav = [[CHSNavigationController alloc] initWithRootViewController:flowcontrolSetting];
    //    [self.navigationController presentViewController:messageSettingNav animated:YES completion:nil];
    [self.navigationController pushViewController:flowcontrolSetting animated:YES];
}

-(void)showOfficeSettingsVC
{
     WZOfficeSettingController*otherSetting = [[WZOfficeSettingController alloc] init];
    //    CHSNavigationController *otherSettingNav = [[CHSNavigationController alloc] initWithRootViewController:otherSetting];
    //    [self.navigationController presentViewController:messageSettingNav animated:YES completion:nil];
    [self.navigationController pushViewController:otherSetting animated:YES];
}

-(void)showOtherSettingsVC
{
    OtherSettingsVC *otherSetting = [[OtherSettingsVC alloc] init];
    //    CHSNavigationController *otherSettingNav = [[CHSNavigationController alloc] initWithRootViewController:otherSetting];
    //    [self.navigationController presentViewController:messageSettingNav animated:YES completion:nil];
    [self.navigationController pushViewController:otherSetting animated:YES];
}

@end